package com.study.app.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.study.app.PageResult;
import com.study.app.Result;
import com.study.app.entity.Info;
import com.study.app.service.IInfoService;
import com.study.app.entity.Params;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.study.app.entity.Student;
import com.study.app.service.IStudentService;

@RestController
@RequestMapping("/app/student")
public class StudentController
{
    @Autowired
    private IStudentService studentService;
    @Autowired
    private IInfoService infoService;

    //查询学生列表
    @GetMapping("/list")
    public PageResult list(Student student)
    {
        IPage<Student> page = new Page<Student>(student.getPageNum(),student.getPageSize());
        IPage<Student> list = studentService.selectStudentList(page,student);
        return PageResult.success(list);
    }


    //获取学生详细信息
    @GetMapping(value = "/{id}")
    public Result getInfo(@PathVariable("id") Long id)
    {

        return Result.success(studentService.selectStudentById(id));
    }

    //获取学生录入信息
    @GetMapping(value = "/getInfo/{id}")
    public Result getInfoT(@PathVariable("id") Long id)
    {
        Student student = studentService.selectStudentById(id);
        String infoId = student.getInfoId();
        if (infoId == null){
            Result result = new Result();
            result.setCode("300");
            return result;
        }
        Info info = infoService.selectInfoById(Long.valueOf(infoId));
        return Result.success(info);
    }

    //新增学生
    @PostMapping
    public Result add(@RequestBody Student student)
    {
        return Result.success(studentService.insertStudent(student));
    }


    //处理分数
    @PostMapping("/handlerScore")
    public Result handlerScore(@RequestBody Params params)
    {
        String data = params.getData();
        String id = params.getId();
        double studentScore = 0;
        JSONObject jsonObject = JSONObject.parseObject(data);
        String year = jsonObject.getString("year");
        // 获取学业分数
        Integer xueyeScore = jsonObject.getInteger("xueyeScore");
        // 获取思政分数

        JSONObject sizheng = jsonObject.getJSONObject("sizheng");
        Integer sizhengScore = 20;
        Integer sizhengKouFen = 0;
        sizhengScore += sizheng.getInteger("daoshiScore"); // 导师分数
         sizhengScore += sizheng.getInteger("rongyuScore"); // 荣誉分数
        Integer koufengScore = sizheng.getInteger("koufengScore"); // 思政扣分类型
        Integer koufengTongBaoNum = sizheng.getInteger("koufengTongBaoNum"); // 思政扣分次数
        if (koufengScore != 0){
            sizhengKouFen = koufengScore * koufengTongBaoNum;
        }
        sizhengScore = sizhengScore - sizhengKouFen;
        if (sizhengScore >= 30){
            // 思政上限30分
            sizhengScore= 30;
        }
        // 获取科研能力分数
        double keyannengli = 0;
        // 学术论文分数
        JSONObject luwenForm = jsonObject.getJSONObject("luwenForm");
        double xueshulunwenScore = 0;
        if (year.equals("3")){
            JSONArray lunwenTableData = luwenForm.getJSONArray("tableData");
            for (Object lunwenTableDatum : lunwenTableData) {
                JSONObject lw = (JSONObject)lunwenTableDatum;
                Integer level = lw.getInteger("level");
                xueshulunwenScore +=level;
            }
            if (xueshulunwenScore >=15 && xueshulunwenScore <=30){
                xueshulunwenScore = xueshulunwenScore * 1.5;
            }
        }else{
            JSONArray lunwenTableData = luwenForm.getJSONArray("tableData");
            for (Object lunwenTableDatum : lunwenTableData) {
                JSONObject lw = (JSONObject)lunwenTableDatum;
                Integer level = lw.getInteger("level");
                xueshulunwenScore += level;
            }

        }

        // 知识产权
        Integer zhishichanquanSumScore = 0;
        JSONObject zhishichanquan = jsonObject.getJSONObject("zhishichanquan");
        JSONArray zhishichanquanScore = zhishichanquan.getJSONArray("zhishichanquanScore");
        if ( zhishichanquanScore.size() > 0){
            for (Object item : zhishichanquanScore) {
                String score = item.toString();
                String[] split = score.split("-");
                String type = split[0];
                Integer typeScore = Integer.valueOf(split[1]);
                Integer typeNum = zhishichanquan.getInteger(type);
                zhishichanquanSumScore += typeNum * typeScore;
            }
        }
        // 科研项目
        Integer keyanxiangmuSumScore = 0;
        JSONObject keyanxiangmu = jsonObject.getJSONObject("keyanxiangmu");
        JSONArray keyanxiangmuScore = keyanxiangmu.getJSONArray("keyanxiangmuScore");
        if ( keyanxiangmuScore.size() > 0){
            for (Object item : keyanxiangmuScore) {
                String score = item.toString();
                String[] split = score.split("-");
                String type = split[0];
                Integer typeScore = Integer.valueOf(split[1]);
                Integer typeNum = zhishichanquan.getInteger(type);
                keyanxiangmuSumScore += typeNum * typeScore;
            }
        }
        // 学科竞赛
        double jibieSumScore = 0;
        JSONObject jingsaiForm = jsonObject.getJSONObject("jingsaiForm");
        JSONArray jingsaiTableData = jingsaiForm.getJSONArray("tableData");
        for (Object jingsai : jingsaiTableData) {
            JSONObject  jingsaiJson = (JSONObject)jingsai;
            String level = jingsaiJson.getString("level");
            String[] split = level.split("-");
            double score = Double.valueOf(split[2]);
            jibieSumScore +=score;
        }

        //        计算总分
        keyannengli = xueshulunwenScore + zhishichanquanSumScore  + keyanxiangmuSumScore + jibieSumScore;
        if (year.equals("2")){
            // 研2
            if (keyannengli >= 30){
                keyannengli =30;
            }
        }else{
            if (keyannengli >= 50){
                keyannengli =50;
            }
        }
        // 社会服务

        Double shehuifuwuScore = Double.valueOf(jsonObject.getString("shehuifuwuScore"));
        JSONObject shehuifuwu = jsonObject.getJSONObject("shehuifuwu");
        Integer shehuifuwukoufengType = shehuifuwu.getInteger("koufenType");
        Integer shehuifuwukoufengNum = shehuifuwu.getInteger("koufengNum"); // 扣分次数
        double shehuifuwuKouFen = 0;
        if (shehuifuwukoufengType != 0){
            //有扣分记录
            shehuifuwuKouFen += shehuifuwukoufengNum * 0.5;

        }
        shehuifuwuScore = shehuifuwuScore-shehuifuwuKouFen;
        if (shehuifuwuScore >= 20){
            shehuifuwuScore = 20.0;
        }
        // 计算学生分数
        studentScore = xueyeScore + sizhengScore + keyannengli + shehuifuwuScore ;
        Student student = studentService.selectStudentById(Long.valueOf(id));
        String infoId = student.getInfoId();
        if (infoId == null || "".equals(infoId)){
            Info info = new Info();
            info.setYear(Long.valueOf(year));
            info.setInfo(data);
            infoService.insertInfo(info);
            student.setInfoId(String.valueOf(info.getId()));
            student.setScore(String.valueOf(studentScore));
            studentService.updateStudent(student);
        }else{
            Info info = infoService.selectInfoById(Long.valueOf(infoId));
            info.setInfo(year);
            info.setInfo(data);
            infoService.updateInfo(info);
            student.setScore(String.valueOf(studentScore));
            studentService.updateStudent(student);
        }


        System.out.println(studentScore);
        return Result.success();
    }

    //修改学生

    @PutMapping
    public Result edit(@RequestBody Student student)
    {
        return Result.success(studentService.updateStudent(student));
    }

    //删除学生

	@DeleteMapping("/{ids}")
    public Result remove(@PathVariable Long[] ids)
    {
        return Result.success(studentService.deleteStudentByIds(ids));
    }
}
